{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "63c1be9a-61d9-43b9-a8d2-c4421ece25a7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from MDXNet import MDXNetDereverb\n",
    "from infer_uvr5 import _audio_pre_, _audio_pre_new\n",
    "from config import Config\n",
    "from tqdm import tqdm\n",
    "import os\n",
    "import traceback, pdb\n",
    "import ffmpeg\n",
    "import torch\n",
    "import shutil\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f98446c1-e3a2-45f7-b61d-00ff929faeef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'代码来源\\n\\nhttps://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI\\nUVR5模型文件\\nhttps://huggingface.co/lj1995/VoiceConversionWebUI/tree/main/uvr5_weights\\n合格的文件夹路径格式举例： E:\\\\codes\\\\py39\\\\vits_vc_gpu\\\\白鹭霜华测试样例(去文件管理器地址栏拷就行了)。\\n模型分为三类：\\n1、保留人声：不带和声的音频选这个，对主人声保留比HP5更好。内置HP2和HP3两个模型，HP3可能轻微漏伴奏但对主人声保留比HP2稍微好一丁点；\\n2、仅保留主人声：带和声的音频选这个，对主人声可能有削弱。内置HP5一个模型；\\n3、去混响、去延迟模型（by FoxJoy）：\\n\\u2003\\u2003(1)MDX-Net(onnx_dereverb):对于双通道混响是最好的选择，不能去除单通道混响；\\n  (234)DeEcho:去除延迟效果。Aggressive比Normal去除得更彻底，DeReverb额外去除混响，可去除单声道混响，但是对高频重的板式混响去不干净。\\n去混响/去延迟，附：\\n    1、DeEcho-DeReverb模型的耗时是另外2个DeEcho模型的接近2倍；\\n    2、MDX-Net-Dereverb模型挺慢的；\\n    3、个人推荐的最干净的配置是先MDX-Net再DeEcho-Aggressive。\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"代码来源\n",
    "\n",
    "https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI\n",
    "UVR5模型文件\n",
    "https://huggingface.co/lj1995/VoiceConversionWebUI/tree/main/uvr5_weights\n",
    "合格的文件夹路径格式举例： E:\\\\codes\\\\py39\\\\vits_vc_gpu\\\\白鹭霜华测试样例(去文件管理器地址栏拷就行了)。\n",
    "模型分为三类：\n",
    "1、保留人声：不带和声的音频选这个，对主人声保留比HP5更好。内置HP2和HP3两个模型，HP3可能轻微漏伴奏但对主人声保留比HP2稍微好一丁点；\n",
    "2、仅保留主人声：带和声的音频选这个，对主人声可能有削弱。内置HP5一个模型；\n",
    "3、去混响、去延迟模型（by FoxJoy）：\n",
    "  (1)MDX-Net(onnx_dereverb):对于双通道混响是最好的选择，不能去除单通道混响；\n",
    "  (234)DeEcho:去除延迟效果。Aggressive比Normal去除得更彻底，DeReverb额外去除混响，可去除单声道混响，但是对高频重的板式混响去不干净。\n",
    "去混响/去延迟，附：\n",
    "    1、DeEcho-DeReverb模型的耗时是另外2个DeEcho模型的接近2倍；\n",
    "    2、MDX-Net-Dereverb模型挺慢的；\n",
    "    3、个人推荐的最干净的配置是先MDX-Net再DeEcho-Aggressive。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f58ea39e-ddea-417d-a7e4-f1e78ec7803f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from process import uvr_prediction,uvr5_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6fc6e013-ef08-4895-abc1-abaab2fa393f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\n['onnx_dereverb_By_FoxJoy',\\n 'HP2_all_vocals',\\n 'HP2-人声vocals+非人声instrumentals',\\n 'HP3_all_vocals',\\n 'HP5_only_main_vocal',\\n 'HP5-主旋律人声vocals+其他instrumentals',\\n 'VR-DeEchoAggressive',\\n 'VR-DeEchoDeReverb',\\n 'VR-DeEchoNormal']\\n\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# python yuki_builder.py crop的-role_audios输出文件夹 带上voice\n",
    "dir_wav_input = '/media/checkpoint/speech_data/video/audio/test/voice/'\n",
    "# 音频说话人的输出文件\n",
    "opt_vocal_root = '/media/checkpoint/speech_data/video/audio/test/output/'\n",
    "# 音频伴奏的输出文件\n",
    "opt_ins_root = '/media/checkpoint/speech_data/video/audio/test/output/'\n",
    "\n",
    "shutil.rmtree(opt_vocal_root, ignore_errors=True)\n",
    "os.makedirs(opt_vocal_root, exist_ok=True)\n",
    "\n",
    "shutil.rmtree(opt_ins_root, ignore_errors=True)\n",
    "os.makedirs(opt_ins_root, exist_ok=True)\n",
    "# 人声提取激进程度\n",
    "agg = 10\n",
    "# 输出音频格式\n",
    "format0 = [\"wav\", \"flac\", \"mp3\", \"m4a\"]\n",
    "\n",
    "# uvr5_weights文件夹下发的模型，uvr5_names变量中存储\n",
    "uvr5_names\n",
    "\"\"\"\n",
    "['onnx_dereverb_By_FoxJoy',\n",
    " 'HP2_all_vocals',\n",
    " 'HP2-人声vocals+非人声instrumentals',\n",
    " 'HP3_all_vocals',\n",
    " 'HP5_only_main_vocal',\n",
    " 'HP5-主旋律人声vocals+其他instrumentals',\n",
    " 'VR-DeEchoAggressive',\n",
    " 'VR-DeEchoDeReverb',\n",
    " 'VR-DeEchoNormal']\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2a5b6fe-02c4-44f6-9339-c95d67f67c29",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aa2d108a-0a48-48da-82fd-399d9cd59704",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.10it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0003_0.00.56.1800_00.00.58.980_为了逃避狼群.wav.reformatted.wav instruments done\n",
      "0003_0.00.56.1800_00.00.58.980_为了逃避狼群.wav.reformatted.wav vocals done\n",
      "clean_empty_cache\n",
      "/media/checkpoint/speech_data/video/audio/test/output/vocal_0003_0.00.56.1800_00.00.58.980_为了逃避狼群.wav.reformatted.wav_10.wav\n",
      "/media/checkpoint/speech_data/video/audio/test/output/instrument_0003_0.00.56.1800_00.00.58.980_为了逃避狼群.wav.reformatted.wav_10.wav\n"
     ]
    }
   ],
   "source": [
    "wav_input = '/media/checkpoint/speech_data/video/audio/test/voice/0003_0.00.56.1800_00.00.58.980_为了逃避狼群.wav'\n",
    "vocal_path, others_path = uvr_prediction(uvr5_names[5], wav_input,\n",
    "                            opt_vocal_root,\n",
    "                            opt_ins_root,\n",
    "                            agg,\n",
    "                            format0[0]\n",
    "                            )\n",
    "print(vocal_path)\n",
    "print(others_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88d46050-e77c-482e-a223-d3e4867aa29f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8646f81e-bea9-44e3-9ef8-94a4f0b71b28",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
